package com.google.code.ptrends.excel.caching;

import java.util.HashMap;
import java.util.Map;

import com.google.code.ptrends.excel.entities.CellStyle;

public final class CellStylePool {

	private static final int DEFAULT_POOL_SIZE = 100;

	private CellStylePool() {
	}

	public static CellStyle getCellStyle(final String background,
			final String foreground, final int fontSize, final boolean isBold,
			final boolean isItalic, final boolean isUnderlined) {

		synchronized (locker) {
			if (pool == null) {
				pool = new HashMap<Integer, CellStyle>(DEFAULT_POOL_SIZE);
			}
		}

		final CellStyle raw = new CellStyle();
		raw.setBackground(background);
		raw.setForeground(foreground);
		raw.setBold(isBold);
		raw.setItalic(isItalic);
		raw.setUnderlined(isUnderlined);
		raw.setFontSize(fontSize);

		final Integer rawKey = raw.hashCode();

		CellStyle result = null;

		if (pool.containsKey(rawKey)) {
			result = pool.get(rawKey);
		} else {
			pool.put(rawKey, raw);
			result = raw;
		}

		return result;
	}

	private static Map<Integer, CellStyle> pool;
	private static Object locker = new Object();
}
